package com.zy.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.common.result.Result;
import com.zy.common.utils.MD5;
import com.zy.model.system.SysUser;
import com.zy.model.vo.SysUserQueryVo;
import com.zy.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Gongxl
 * @since 2023-06-07
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

    @ApiOperation("用户列表")
    @GetMapping( "/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       SysUserQueryVo sysUserQueryVo) {
//创建page对象
        Page<SysUser> pageParam = new Page<>(page,limit);//调用service方法
        IPage<SysUser> pageModel = sysUserService.selectPage(pageParam, sysUserQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("添加用户")
    @PostMapping( "save")
    public Result save(@RequestBody SysUser user) {
        //默认可用
        user.setStatus(1);
        //MD5加密
        String encrypt = user.getPassword();
        user.setPassword(MD5.encrypt(encrypt));

        boolean is_success = sysUserService.save(user);
        if(is_success) {
            return Result.ok( );
        }else {
            return Result.fail();
        }

    }

    @ApiOperation("根据id查询")
    @GetMapping( "getUser/{id}")
    public Result getUser(@PathVariable String id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }
    @ApiOperation("修改用户")
    @PostMapping( "update")
    public Result update(@RequestBody SysUser user) {
        boolean is_success = sysUserService.updateById(user);
        if(is_success) {
            return Result.ok( );
        }else {
            return Result.fail( );
        }
    }
    @ApiOperation("删除用户")
    @DeleteMapping( "remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean is_Success = sysUserService.removeById(id);
        if (is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }
}

